import {
    BufferGeometry, MeshStandardMaterial,
    Mesh, BufferAttribute, Color
} from 'three'
import {VertexNormalsHelper} from 'three/examples/jsm/helpers/VertexNormalsHelper'

import {picTexture} from './loader'

const size: number = 10;

//定义每个面的顶点, 每个面四个顶点
const points: Float32Array = new Float32Array([
    //    上面 四个顶点
    -size, size, size, //顶点0
    size, size, size, //顶点1
    size, size, -size,
    -size, size, -size,

//    下面
    -size, -size, size,
    size, -size, size,
    size, -size, -size,
    -size, -size, -size,

    //    左面
    -size, size, size,
    -size, size, -size,
    -size, -size, -size,
    -size, -size, size,

    //    右面
    size, size, size,
    size, size, -size,
    size, -size, -size,
    size, -size, size,

    //前 面 的4个顶点绘制面
    -size, size, size,  //p16
    -size, -size, size, //p17
    size, -size, size,  //p18
    size, size, size,   //p19

//    后  面 的4个顶点
    -size, size, -size,
    -size, -size, -size,
    size, -size, -size,
    size, size, -size,

])

//法线 数据
const normals: Float32Array = new Float32Array([

    0,1,0, //上面 四个顶点
    0,1,0,
    0,1,0,
    0,1,0,

    0,-1,0,
    0,-1,0,
    0,-1,0,
    0,-1,0,

    -1,0,0, //左边顶点
    -1,0,0,
    -1,0,0,
    -1,0,0,

    1,0,0,
    1,0,0,
    1,0,0,
    1,0,0,

    0,0,1, //前面的四个顶点的法线 向量
    0,0,1,
    0,0,1,
    0,0,1,

    0,0,-1,
    0,0,-1,
    0,0,-1,
    0,0,-1,
])

//uv 数据(6个面, 每个面一样)
const uvs: Float32Array = new Float32Array([
    0,0,
    1,0,
    1,1,
    0,1,

    0,0,
    1,0,
    1,1,
    0,1,

    0,0,
    1,0,
    1,1,
    0,1,

    0,0,
    1,0,
    1,1,
    0,1,

    0,0,
    1,0,
    1,1,
    0,1,

    0,0,
    1,0,
    1,1,
    0,1,
])


//与points上的点对应, 数字对应顶点的索引
const indexs: number[] = [
    0,1,2, //上面的四个顶点组成
    2,3,0,

    4,7,6,//下
    6,5,4,

    8,9,10,
    10,11,8,

    12,15,14,
    14,13,12,

    16,17,18,
    18,19,16,

    21,20,23,
    23,22,21
]

const geometryBox: BufferGeometry = new BufferGeometry();

geometryBox.setAttribute('position', new BufferAttribute(points, 3))
geometryBox.setAttribute('normal', new BufferAttribute(normals, 3,))
geometryBox.setAttribute('uv', new BufferAttribute(uvs, 2))

geometryBox.setIndex(indexs)

//自定义物体
const box:Mesh = new Mesh(
    geometryBox,
    new MeshStandardMaterial({
        color: '#fff',
        map: picTexture
    })
)

const boxHelper: VertexNormalsHelper = new VertexNormalsHelper(box, 10, new Color('orange').getHex())

// box.rotateX(Math.PI/180*90)

export default [
    box,
    boxHelper
]